package com.troyhigh.njrotc.admintrackerold;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.sql.Date;

public class Event implements Comparable
{
    private String name;
    private Date date;
    private Category category;
    private Properties props;
    private Hashtable<Integer, Integer> table;
    
    public Event(String n, Date d, Category c)
    {
        this(n, d, c, null);
    }
    
    public Event(String n, Date d, Category c, Properties p)
    {
        name = n;
        date = d;
        category = c;
        props = p;
        table = new Hashtable<Integer, Integer>();
    }
    
    public String getName()
    {
        return name;
    }
    
    public Date getDate()
    {
        return date;
    }
    
    public Category getCategory()
    {
        return category;
    }
    
    public Properties getProperties()
    {
        return props;
    }
    
    //Hashtable functions
    
    public void loadTable(Hashtable<Integer, Integer> tbl)
    {
        table = tbl;
        if(tbl == null)
            table = new Hashtable<Integer, Integer>();
    }
    
    public void add(int stu, int val)
    {
        table.put(stu, val);
    }
    
    public void add(Student stu, int val)
    {
        table.put(stu.getID(), new Integer(val));
    }
    
    public int get(int stu)
    {
        Integer val = table.get(stu);
        if(val == null || val < 0) 
            return 0;
        int v = val.intValue();
        int c = Integer.parseInt(props.getProperty("countingvalue", "0"));
        return (v <= c) ? v : c;
    }
    
    public int get(Student stu)
    {
        return get(stu.getID());
    }
    
    public ArrayList<Integer> getIDs()
    {
        ArrayList<Integer> r = new ArrayList<Integer>();
        Enumeration<Integer> e = table.keys();
        while(e.hasMoreElements())
            r.add(e.nextElement());
        return r;
    }
    
    public void remove(Student stu)
    {
        table.remove(stu.getID());
    }
    
    //General methods
    @Override
    public String toString()
    {
        return "Event: " + name +
               ", Started: " + date +
               ", Category: " + category;
    }
    
    public String PID() //do not change! changing this WILL destroy the records.
    {
         String namehash = (name.length() > 5) ? name.substring(0,5).toLowerCase() : name;
         String datehash = "" + date.getTime();
         String pid = namehash.trim().replace(" ", "_") + datehash;
         return pid;
    }
    
    public int compareTo(Object o)
    {
        Event that = ((Event)o);
        int d = this.getDate().compareTo(that.getDate());
        int n = this.getName().compareTo(that.getName());
        if(d == 0)
            return n;
        return d;
    }

}